package com.bwie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.Group;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupActive;
import com.bwie.mapper.GroupActiveMapper;
import com.bwie.service.GroupActiveService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class GroupActiveServiceImpl extends ServiceImpl<GroupActiveMapper, GroupActive> implements GroupActiveService{

    @Autowired
    GroupActiveMapper groupActiveMapper;
    @Override
    public R getGroupActiveList(PageInfoVo pageInfoVo) {
        Page<GroupActive> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        QueryWrapper<GroupActive> groupActiveQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            groupActiveQueryWrapper.lambda().like(GroupActive::getActiveName,pageInfoVo.getKeyWord());
        }
        Page<GroupActive> groupActivePage = groupActiveMapper.selectPage(page, groupActiveQueryWrapper);
        return R.OK(groupActivePage);
    }

    @Override
    public R addGroupActive(GroupActive groupActive) {
        GroupActive groupActive1 = groupActiveMapper.selectOne(
                new QueryWrapper<GroupActive>().lambda().eq(GroupActive::getActiveName, groupActive.getActiveName())
        );
        if(ObjectUtils.isNotEmpty(groupActive1)) {
            return R.ERROR(500, "活动已存在");
        }
        List<String> getTimeList = groupActive.getGetTimeList();
        groupActive.setBeginTime(getTimeList.get(0));
        groupActive.setEndTime(getTimeList.get(1));
        groupActiveMapper.insert(groupActive);
        return R.OK();
    }

    @Override
    public R updateActive(GroupActive groupActive) {
        groupActiveMapper.updateById(groupActive);
        return R.OK();
    }

    @Override
    public R deleteActive(Integer activeId) {
        GroupActive groupActive = groupActiveMapper.selectById(activeId);
        if(ObjectUtils.isEmpty(groupActive)){
            return R.ERROR(500,"订单不存在");
        }
        groupActiveMapper.deleteById(activeId);
        return R.OK();
    }

    @Override
    public R selectGroupActive() {
        List<GroupActive> groupActives = groupActiveMapper.selectList(null);
        return R.OK(groupActives);
    }
}
